package com.example.bankend.service.impl;

import com.example.bankend.entity.Card;
import com.example.bankend.entity.Trade;
import com.example.bankend.entity.User;
import com.example.bankend.mapper.CardMapper;
import com.example.bankend.mapper.TradeMapper;
import com.example.bankend.mapper.UserMapper;
import com.example.bankend.service.CardService;
import com.example.bankend.service.TextService;
import com.example.bankend.service.TradeService;
import com.example.bankend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luolinila
 * @date 2025/6/18 15:29
 */
@Service
public class TradeServerIpml implements TradeService {
    @Autowired
    private TradeMapper tradeMapper;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String Withdraw(String cardID, BigDecimal money, String pass) {
        try{
            Card card = cardMapper.getCardByID(cardID);
            if (card == null || "1".equals(card.getIsDelete()) || "1".equals(card.getIsReportLoss())) {
                return "10004";
            }
            if (!card.getPass().equals(pass)) {
                return "10005";
            }
            if (card.getBalance().compareTo(money) < 0) {
                return "10006";
            }
            cardMapper.updateBalance(card.getCardID(), card.getBalance().subtract(money));
            Trade trade = new Trade("取款", money, "无", cardID);
            tradeMapper.insertTrade(trade);
            return "200";
        } catch (Exception e) {
            System.out.println(e);
            return "-1";
        }
    }

    @Override
    public String saveMoney(String cardID, BigDecimal money, String pass) {
        try {
            Card card = cardMapper.getCardByID(cardID);
            if (card == null || "1".equals(card.getIsDelete()) || "1".equals(card.getIsReportLoss())) {
                return "10004";
            }
            if (!card.getPass().equals(pass)) {
                return "10005";
            }
            cardMapper.updateBalance(card.getCardID(), card.getBalance().add(money));
            Trade trade = new Trade("存款", money, "无", cardID);
            tradeMapper.insertTrade(trade);
            return "200";
        } catch (Exception e) {
            System.out.println(e);
            return "-1";
        }
    }

    @Override
    public String transfer(String fromCardID, String toCardID, BigDecimal money, String pass) {
        try{
            Card fromCard = cardMapper.getCardByID(fromCardID);
            Card toCard = cardMapper.getCardByID(toCardID);
            if (fromCard == null || toCard == null
                    || "1".equals(fromCard.getIsDelete()) || "1".equals(toCard.getIsDelete())
                    || "1".equals(fromCard.getIsReportLoss()) || "1".equals(toCard.getIsReportLoss())) {
                return "10004";
            }
            if (!pass.equals(fromCard.getPass())) {
                return "10005";
            }
            if (fromCard.getBalance().compareTo(money) < 0) {
                return "10006";
            }
            cardMapper.updateBalance(fromCard.getCardID(), fromCard.getBalance().subtract(money));
            Trade fromTrade = new Trade("转出", money, "无", fromCardID);
            tradeMapper.insertTrade(fromTrade);
            cardMapper.updateBalance(toCard.getCardID(), toCard.getBalance().add(money));
            Trade toTrade = new Trade("转入", money, "无", toCardID);
            tradeMapper.insertTrade(toTrade);
            return "200";
        } catch (Exception e) {
            System.out.println(e);
            return "-1";
        }
    }

    @Override
    public Long deleteTrade(Integer tradeID, Long adminID, String adminName, String adminPass) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return -1L;
            }
            Trade trade = tradeMapper.getTradeById(tradeID);
            if (trade == null || "1".equals(trade.getIsDelete())) {
                return 10004L;
            }
            tradeMapper.deleteTrade(tradeID);
            return 200L;
        } catch (Exception e) {
            System.out.println(e);
            return -1L;
        }
    }



    @Override
    public List<Trade> selectAllTrade(Long adminID, String adminName, String adminPass, Long customerID) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                System.out.println("aaaaa");
                return null;
            }
            User user = userMapper.selectByCunstomerID(customerID);
            if (user == null || "1".equals(user.getIsDelete())) {
                return null;
            }
            List<String> allCards = cardMapper.selectAllCardIDByID(user.getCustomerID());
            List<Trade> resList = new ArrayList<>();
            for (String cardID : allCards) {
                List<Trade> list = tradeMapper.selectAllByCardID(cardID);
                System.out.println(list);
                resList.addAll(list);
            }
            return resList;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public int deleteAllByCardID(String cardID) {
        try {
            Card card = cardMapper.getCardByID(cardID);
            if(card == null) {
              return -1;
            }
            tradeMapper.deleteallByCardID(cardID);
            return 200;
        } catch (Exception e) {
            System.out.println();
            return -1;
        }
    }

    @Override
    public int restoreAllByCardID(String cardID) {
        try {
            Card card = cardMapper.getCardByID(cardID);
            if(card == null) {
                return -1;
            }
            tradeMapper.restoreAllByCardID(cardID);
            return 200;
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }

    @Override
    public List<Trade> getAllTrade(Long adminID, String adminName, String adminPass) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return null;
            }
            return tradeMapper.selectAll();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }
}
